<?php

namespace App\Services;

use App\Exceptions\InvalidRequestException;
use App\Models\Advertisement;
use Carbon\Carbon;
use Illuminate\Support\Facades\URL;

class AdvertisementService
{

    public function adv()
    {
        if (request()->has('location')) {
            $types = is_array(request()->input('location'))
                ? request()->input('location')
                : [request()->input('location')];
        } else {
            $types = array_keys(Advertisement::$locationMap);
        }

        $map = [
            ['start_time', '<=', Carbon::now()],
            ['end_time', '>=', Carbon::now()],
            ['status', "=", true],
        ];

        $adv = []; // 存放要返回的广告

        if (in_array(Advertisement::LOCATION_INDEX_HOT, $types)) {
            $adv['index_hot'] = Advertisement::query()
                ->where('location', Advertisement::LOCATION_INDEX_HOT)
                ->where($map)
                ->get()
                ->each(
                    function ($banner) {
                        $banner->cover = env('APP_URL') .'storage/'.$banner->cover;

                        return $banner;
                    }
                );
        }

        if (in_array(Advertisement::LOCATION_INDEX_NEW, $types)) {
            $adv['index_new'] = Advertisement::query()
                ->where('location', Advertisement::LOCATION_INDEX_NEW)
                ->where($map)
                ->get()
                ->each(
                    function ($banner) {
                        $banner->cover = env('APP_URL') .'storage/'.$banner->cover;

                        return $banner;
                    }
                );
        }

        if (in_array(Advertisement::LOCATION_INDEX_TOP, $types)) {
            $adv['index_top'] = Advertisement::query()
                ->where('location', Advertisement::LOCATION_INDEX_TOP)
                ->where($map)
                ->get()
                ->each(
                    function ($banner) {
                        $banner->cover = env('APP_URL').'storage/'.$banner->cover;
                        return $banner;
                    }
                );
        }

        if (in_array(Advertisement::LOCATION_INDEX_RECOMMEND, $types)) {
            $adv['index_recommend'] = Advertisement::query()
                ->where('location', Advertisement::LOCATION_INDEX_RECOMMEND)
                ->where($map)
                ->get()
                ->each(
                    function ($banner) {
                        $banner->cover = env('APP_URL').'storage/'.$banner->cover;

                        return $banner;
                    }
                );
        }

        if (in_array(Advertisement::LOCATION_CATEGORY_TOP, $types)) {
            $adv['category_top'] = Advertisement::query()
                ->where('location', Advertisement::LOCATION_CATEGORY_TOP)
                ->where($map)
                ->get()
                ->each(
                    function ($banner) {
                        $banner->cover = env('APP_URL').'storage/'.$banner->cover;

                        return $banner;
                    }
                );
        }

        if (!$adv) {
            throw new InvalidRequestException('类型异常');
        }

        return $adv;
    }
}